<template>
  <el-dialog
    :title="formData.id ? '编辑房产' : '新增房产'"
    :visible.sync="showDialog"
    width="60%"
    :before-close="closeFn"
  >
    <!-- form表单添加表单验证 -->
    <el-form ref="form" :rules="rules" :model="formData" label-width="120px">
      <el-form-item label="经纪">
        <el-select
          v-model="formData.agent"
          style="width: 30%"
          placeholder="请选择房产"
        >
          <el-option
            v-for="item in agentList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="分类">
        <el-select
          v-model="formData.category"
          style="width: 30%"
          placeholder="请选择分类"
        >
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="标题" prop="title">
        <el-input
          v-model="formData.title"
          style="width: 80%"
          placeholder="请输入1-50个字符"
        />
      </el-form-item>

      <el-form-item label="标签">
        <el-tag
          v-for="tag in formData.tags"
          :key="tag"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput"
          >+</el-button
        >
      </el-form-item>

      <el-form-item label="地址">
        <el-cascader
          v-model="selectedOptions"
          size="large"
          :options="options"
          placeholder="请选择地址"
          style="width: 30%"
          @change="handleChange"
        />
      </el-form-item>

      <el-form-item label="价格" prop="price">
        <el-input
          v-model="formData.price"
          style="width: 80%"
          placeholder="请输入1-10个数字"
        />
      </el-form-item>

      <el-form-item label="描述" prop="desc">
        <el-input
          v-model="formData.desc"
          :rows="4"
          type="textarea"
          style="width: 80%"
          placeholder="请输入1-200个字符"
        />
      </el-form-item>

      <el-form-item label="内容">
        <VueEditor v-model="formData.content" style="width: 80%" />
      </el-form-item>

      <el-form-item label="介绍图片">
        <ImgUpload ref="img1" />
      </el-form-item>

      <el-form-item label="封面">
        <ImgUpload ref="img2" :limit="1" />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <!-- 给取消按钮绑定点击事件 -->
      <el-button @click="closeFn">取 消</el-button>
      <!-- 确定按钮添加点击事件 -->
      <el-button type="primary" @click="confrimFn">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import ImgUpload from "./imgUpload.vue";
import { VueEditor } from "vue2-editor";
import { regionData, CodeToText, TextToCode } from "element-china-area-data";
import {
  getManagerList,
  getCategoryList,
  addProperty,
  getPropertyById,
  editProperty,
} from "@/api/property";
export default {
  components: {
    VueEditor,
    ImgUpload,
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      formData: {
        title: "", // 标题
        desc: "", // 描述
        image: "", // 大图 封面
        agent: "", // 经纪人id
        category: "", // 分类id
        content: "", // 内容
        address: "", // 地址
        price: "", // 价格
        photos: [], // 小图 介绍封面
        tags: [], // 标签数组
      },
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          { min: 1, max: 20, message: "标题字符1-20内", trigger: "blur" },
        ],
        price: [{ required: true, message: "请输入价格", trigger: "blur" }],
        desc: [
          { required: true, message: "请输入描述内容", trigger: "blur" },
          { min: 1, max: 200, message: "描述内容字符1-200内", trigger: "blur" },
        ],
      },
      agentList: [], // 经济数组
      categoryList: [], // 分类数组
      inputVisible: false, // 控制tag中按钮或输入框显示
      inputValue: "", // tag中输入框的值
      options: regionData, // 省市县数据
      selectedOptions: [], // 省市县下拉框显示数据
    };
  },
  async created() {
    this.agentList = await getManagerList();
    this.categoryList = await getCategoryList();
  },
  methods: {
    async confrimFn() {
      const img1 = this.$refs.img1.fileList;
      this.formData.photos = img1.map((item) => item.url);
      const img2 = this.$refs.img2.fileList;
      const img3 = img2.map((item) => item.url);
      this.formData.image = img3.toString();

      if (this.formData.id) {
        await editProperty(this.formData);
        this.$message.success("编辑成功");
      } else {
        await addProperty(this.formData);
        this.$message.success("新增成功");
      }
      this.$emit("updateEvent");

      this.closeFn();
    },
    closeFn() {
      this.formData = {
        title: "", // 标题
        desc: "", // 描述
        image: "", // 大图 封面
        agent: "", // 经纪人id
        category: "", // 分类id
        content: "", // 内容
        address: "", // 地址
        price: "", // 价格
        photos: [], // 小图 介绍封面
        tags: [], // 标签数组
      };
      this.$refs.img1.fileList = [];
      this.$refs.img2.fileList = [];
      this.selectedOptions = [];
      this.selectedOptions = [];
      this.$emit("update:showDialog", false);
    },
    // 获取指定房产
    async getPropertyById(id) {
      this.formData = await getPropertyById(id);
      this.formData.agent = this.formData.agent.id;
      this.formData.category = this.formData.category.id;
      const arr = this.formData.address;
      const a = arr.split(",")[0];
      const b = arr.split(",")[1];
      const c = arr.split(",")[2];
      this.selectedOptions = TextToCode[a][b][c].code;

      //  回显图片
      const photos = this.formData.photos;
      const image = this.formData.image;
      const obj = photos.map((item) => {
        return { url: item };
      });
      this.$refs.img1.fileList.push(...obj);

      //  回显封面
      const obj2 = {};
      obj2["url"] = image;
      this.$refs.img2.fileList.push(obj2);
    },

    // tags事件
    handleClose(tag) {
      this.formData.tags.splice(this.formData.tags.indexOf(tag), 1);
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      const inputValue = this.inputValue;
      if (inputValue) {
        this.formData.tags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },

    // 格式化省市县数据
    handleChange(value) {
      console.log(value);
      const res = value.map((item) => {
        return CodeToText[item];
      });
      this.formData.address = res.toString();
    },
  },
};
</script>

<style  scoped>
::v-deep .el-dialog__footer {
  text-align: center;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>

